package com.weidong.mybbs.service;

import com.weidong.mybbs.entity.Permission;
import com.weidong.mybbs.entity.RolePermissionRelation;
import com.weidong.mybbs.entity.UserRole;
import com.weidong.mybbs.entity.UserRoleRelation;
import com.weidong.mybbs.filter.PermissionFilter;
import com.weidong.mybbs.filter.RolePermissionRelationFilter;
import com.weidong.mybbs.filter.UserRoleRelationFilter;
import com.weidong.mybbs.mapper.PermissionMapper;
import com.weidong.mybbs.mapper.RolePermissionRelationMapper;
import com.weidong.mybbs.mapper.UserRoleMapper;
import com.weidong.mybbs.mapper.UserRoleRelationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 权限service
 */
@Service
public class PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private RolePermissionRelationMapper rolePermissionRelationMapper;
    @Autowired
    private UserRoleRelationMapper userRoleRelationMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 验证用户权限
     * @return
     */
    public boolean validPermission(int userId,String key){
        //获取所有用户分配的角色
        UserRoleRelationFilter userRoleRelationFilter = new UserRoleRelationFilter();
        userRoleRelationFilter.setUserId(userId);
        List<UserRoleRelation> allRoles = userRoleRelationMapper.findList(userRoleRelationFilter);
        if (allRoles == null || allRoles.size() == 0){
            return false;
        }
        //先判断是否为超管
        for (UserRoleRelation urr:allRoles) {
            //获取该角色信息，判断是否为超管
            UserRole role = userRoleMapper.findById(urr.getRoleId());
            if (role.isAdmin()){
                return true;
            }
            RolePermissionRelationFilter rolePermissionRelationFilter = new RolePermissionRelationFilter();
            rolePermissionRelationFilter.setRoleId(urr.getRoleId());
            List<RolePermissionRelation> rolePermissionRelations = rolePermissionRelationMapper.findList(rolePermissionRelationFilter);
            if (rolePermissionRelations != null && rolePermissionRelations.size() > 0){
                for (RolePermissionRelation rpr:rolePermissionRelations) {
                    Permission permission = permissionMapper.findById(rpr.getPermissionId());
                    if (permission != null && key.equals(permission.getKey())){
                        return true;
                    }
                }
            }
        }

        return false;
    }

    /**
     * 获取用户权限
     * @return
     */
    public Set<String> getUserPermissions(int userId){
        Set<String> ps = new HashSet<>();
        //获取所有用户分配的角色
        UserRoleRelationFilter userRoleRelationFilter = new UserRoleRelationFilter();
        userRoleRelationFilter.setUserId(userId);
        List<UserRoleRelation> allRoles = userRoleRelationMapper.findList(userRoleRelationFilter);
        if (allRoles == null || allRoles.size() == 0){
            return ps;
        }
        //先判断是否为超管
        for (UserRoleRelation urr:allRoles) {
            //获取该角色信息，判断是否为超管
            UserRole role = userRoleMapper.findById(urr.getRoleId());
            if (role.isAdmin()){
                //超管获取所有权限
                PermissionFilter filter = new PermissionFilter();
                List<Permission> allPs = permissionMapper.findList(filter);
                if (allPs != null && allPs.size() > 0){
                    ps.addAll(allPs.stream().map(Permission::getKey).collect(Collectors.toList()));
                }
                return ps;
            }
            RolePermissionRelationFilter rolePermissionRelationFilter = new RolePermissionRelationFilter();
            rolePermissionRelationFilter.setRoleId(urr.getRoleId());
            List<RolePermissionRelation> rolePermissionRelations = rolePermissionRelationMapper.findList(rolePermissionRelationFilter);
            if (rolePermissionRelations != null && rolePermissionRelations.size() > 0){
                for (RolePermissionRelation rpr:rolePermissionRelations) {
                    Permission permission = permissionMapper.findById(rpr.getPermissionId());
                    if (permission != null){
                        ps.add(permission.getKey());
                    }
                }
            }
        }
        return ps;
    }
}
